<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Question_model extends CI_Model {

	var $title   = '';
	var $content = '';
	var $date    = '';

	function __construct()
	{
		parent::__construct();
	}

	function get_all_question($num,$offset)
	{
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$query = $this->db->get('tbl_question',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}

	function list_all_question()
	{
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}
	function questiondetail($id=null){
		$this->load->database();
		$this->db->where("id",$id); 
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}
	function get_details_question($title){
		$this->load->database(); 
	 
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				if(mb_strtolower(url_title($this->removesign($result->content_question))) == $title){
						
					return $result->id;
				}
			}
		} else {
			return array();
		}
	}
	function list_all_question_active()
	{
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$this->db->where('status',1);
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}
	
	function list_new_question()
	{
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$this->db->limit(6);
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}

	function total_question(){
		$this->load->database();
		return $this->db->count_all('tbl_question');
	}
	function delete_question($id)
	{
		$this->load->database('default');
		$this->db->delete('tbl_question', array('id' => $id));
	}

	function insert_question($cateid,$fullname,$email,$title_question,$content_question)
	{
		$this->load->database('default');
		$data = array(
			'cateid' => $cateid ,                
			'datecreate' =>  date("Y-m-d H:i:s"),
        	'fullname' => $fullname,
        	'email' => $email,
        	'title_question' =>  $title_question ,
        	'content_question' =>  $content_question,
        	'status' => 0
		);
		$this->db->insert('tbl_question', $data);
	}

	function update_question_status($id){
		$this->load->database();
		$data = array(
				'status'=>1,  
		);
		$this->db->where('id', $id);
		$this->db->update('tbl_question', $data);
	}

	function get_question_by_cate($cate_id)
	{
		$cate_id = 1; //test
		$this->load->database();
		$this->db->where('cateid',$cate_id);
		$this->db->where('status',1);
		$query = $this->db->get('tbl_question');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		} else {
			return array();
		}
	}
	
function removesign($str)
	{
		$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
		,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à");
				$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a");
				return str_replace($coDau,$khongDau,$str);
	}

}